package com.osfw.dbadminer.controller.database;

import com.osfw.framework.annotation.Log;
import com.osfw.framework.exception.ServiceException;
import com.osfw.framework.enums.BusinessType;
import com.osfw.dbadminer.domain.database.HintSqlParam;
import com.osfw.dbadminer.domain.database.TableDef;
import com.osfw.dbadminer.service.database.IDbaDataTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * 数据表操作
 */
@Controller
@RequestMapping("/osfw/dbadminer/database/table")
public class DbaDatabaseTableController {
    private String prefix = "osfw/dbadminer/database";

    @Autowired
    private IDbaDataTableService dataTableService;


    @GetMapping("/{connectId}/{connectName}/{databaseName}")
    public String allTables(@PathVariable Long connectId, @PathVariable String connectName, @PathVariable String databaseName, ModelMap mmap) {

        List<TableDef> tables = dataTableService.getAllTables(connectId, databaseName);
        Integer maxRem = tables.isEmpty() ? 0 : tables.stream().map(x -> x.getTableName())
                    .max(Comparator.comparingInt(x -> x.length())).orElse("").length();

        mmap.put("connectId", connectId);
        mmap.put("connectName", connectName);
        mmap.put("databaseName", databaseName);
        mmap.put("tables", tables);
        mmap.put("maxrem", maxRem + 15);

        return prefix + "/table";
    }

    @Log(title = "获取数据表字段提示数据", businessType = BusinessType.GRANT)
    @PostMapping("/hint")
    @ResponseBody
    public Map<String, Object> hint(HintSqlParam hintSqlParam) {
        Map<String, Object> hintData = dataTableService.getAllHintData(hintSqlParam);
        return hintData;
    }

    @Log(title = "查询数据库表", businessType = BusinessType.GRANT)
    @GetMapping("/list/{connectId}/{database}")
    @ResponseBody
    public List<TableDef> list(@PathVariable("connectId") Long connectId, @PathVariable("database") String database) {
        List<TableDef> tables = dataTableService.getAllTables(connectId, database);
        return tables;
    }

    @GetMapping("/add")
    public String add(ModelMap mmap) {
        return prefix + "/add";
    }

    @Log(title = "查看数据表结构", businessType = BusinessType.GRANT)
    @GetMapping("/structure/{connectId}/{database}/{tableName}")
    @ResponseBody
    public Map<String, List<String>> structure(@PathVariable("connectId") Long connectId, @PathVariable("database") String database, @PathVariable("tableName") String tableName) {
        try {
            Map<String, List<String>> structure = dataTableService.getStructure(connectId, database, tableName);
            return structure;
        } catch (SQLException e) {
            e.printStackTrace();
            ServiceException.throw500("查询异常");
        }
        return null;
    }

}
